$(function(){
    var fn = ABUS.Fn;

    $("#order_info").renderTemp({
        data:__param__
    });

    fn.countDown($("[data-time]:first").data("time"),function (time,min) {
        $("[data-time]:first").text(min+"分钟");
    });
    fn.countDown($("[data-time1]:first").data("time1"),function (time,min) {
        $("[data-time1]:first").text("余:"+min+"分钟");
    });
    $("#order_info").on("touchend", ".pic_list li",function () {
        //新增图片
        var that = $(this);
        var $el = $("#order_info").find(".pic_list");
        if($(this).is(".addPic")){
            fn.wxPhoto({
                size:1,
                success: function (localIds, serverId) {
                    that.before('<li><img data-serverId="'+serverId+'" src="' + localIds[0] + '"/></li>');
                    if($el.find("li:not(.addPic) img").length >= 3){
                        $el.find("li.addPic").remove();
                    }
                }
            });
        }else{
            //更换图片
            fn.wxPhoto({
                size:1,
                success: function (localIds, serverId) {
                    that.empty().append('<img data-serverId="'+serverId+'" src="' + localIds[0] + '"/>');
                }
            });
        }
    });
    $(".bottom_button .bottom_left").on("touchend",function () {
        fn.page_goto({
            url:"./yewuyuan_banli_remarks.html",
            data:__param__
        })
    });
    $(".bottom_button .bottom_right").on("touchend",function () {

        fn.confirm({
            onSubmit: function () {
                var paymentFee = $.trim($('#jiaofei').val());
                var returnedFee = $.trim($('#tuihuan').val());

                if($('#jiaofei').length > 0 && (paymentFee === '' || /\D/.test(paymentFee))){
                    fn.alert({
                        type:"error",
                        title:"请输入缴费金额"
                    });
                    return false;
                }else if($('.add_picture').length > 0 && $("#order_info").find(".pic_list li:not(.addPic)").length === 0){
                    fn.alert({
                        type:"error",
                        title:"请上传照片"
                    });
                    return false;
                }else if($('#tuihuan').length > 0 && (returnedFee === ''|| /\D/.test(returnedFee))){
                    fn.alert({
                        type:"error",
                        title:"请输入退还金额"
                    });
                    return false;
                }

                var mediaIds = [];
                $("#order_info").find(".pic_list li:not(.addPic) img").each(function () {
                    mediaIds.push($(this).attr('data-serverId'));
                });
                fn.get({
                    url:"/common/order/finishChildOrder",
                    data:{
                        orderId	 : __param__.orderId,//订单id
                        childOrderId : __param__.id,//	子订单id
                        isPaiedChildOrder: "1",//	是否为结算订单	Y	1是0否
                        returnedFee : returnedFee?returnedFee:0,//	退还金额	N	isPaiedChildOrder为1时有该字段
                        paymentFee : paymentFee?paymentFee:0,//	缴费金额	N
                        remainTime	: __param__.remainTime,//剩余时间	Y
                        childOrderRemainTime: __param__.childOrderRemainTime,
                        mediaIds : mediaIds.join(',')
                    },
                    success:function (data) {
                        if(data.result == 1){
                            if(__param__.pageType === 'zuzhang'){
                                //如果是待付款
                                if(__param__.state == '2'){
                                    fn.page_goto({
                                        url:"../../zuzhang/banli/zuzhang_banli_leaderSettle.html",
                                        data:__param__
                                    })
                                }else{
                                    fn.page_goto({
                                        url:"../../zuzhang/banli/zuzhang_banli_shouye.html"
                                    })
                                }
                            }else{
                                fn.page_goto({
                                    url:"./yewuyuan_banli_shouye.html"
                                })
                            }
                        }else{
                            fn.alert({
                                type:"error",
                                title:data.message
                            })
                        }
                    }
                });
                fn.closeModal();
            },
            onCancel: function () {
                fn.closeModal();
            }
        })

    });
    $(document.body).on("input","textarea",function () {
        var len =$(this).val().length;
        if(len > 200){
            return false;
        }
        $(".font_length").text(len + "/200");
    })

});